/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.thienloc.omap.controller.report;

import com.thienloc.omap.controller.BaseBean;
import com.thienloc.omap.entity.BoSoTrungThuong;
import com.thienloc.omap.entity.report.LotteryStatistic;
import com.thienloc.omap.entity.TinhThanh;
import com.thienloc.omap.session.BoSoTrungThuongFacade;
import com.thienloc.omap.session.TinhThanhFacade;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

/**
 *
 * @author GiangLT
 */
@ManagedBean
@ViewScoped
public class LotteryStatBean extends BaseBean implements Serializable {

    @EJB
    private TinhThanhFacade tinhThanhFacade;
    @EJB
    private BoSoTrungThuongFacade boSoTrungThuongFacade;
    private String selectedProvince;
    private Map<String, String> provinceMap;
    private List<LotteryStatistic> list = new ArrayList<LotteryStatistic>();
    private final Logger logger = LoggerFactory.getLogger(LotteryStatBean.class);

    @PostConstruct
    public void init() {
        try {
            List<TinhThanh> tinhThanhs = tinhThanhFacade.findAll();
            provinceMap = new TreeMap<String, String>();
            for (TinhThanh tinhThanh : tinhThanhs) {
                provinceMap.put(tinhThanh.getTenTinh() + " - " + tinhThanh.getMaTinh(), tinhThanh.getMaTinh());
            }
        } catch (Exception ex) {
            logger.error("[{}]: ", getUsername(), ex);
        }
    }

    public void search() {
        Map<String, LotteryStatistic> lotteryStatMap = new TreeMap<String, LotteryStatistic>();
        //Tao san 99 bo so
        for (int i = 0; i < 100; i++) {
            String num = String.valueOf(i);
            if (i < 10) {
                num = "0" + num;
            }
            LotteryStatistic ls = new LotteryStatistic();
            ls.setNumber(num);
            lotteryStatMap.put(num, ls);
        }

        List<BoSoTrungThuong> bsttList = boSoTrungThuongFacade.findByMaTinh(selectedProvince);
        for (BoSoTrungThuong boSoTrungThuong : bsttList) {
            List<String> cacBoSo = Arrays.asList(boSoTrungThuong.getBoSoTrungThuongPK().getBoSo().split(","));
            for (String boSo : lotteryStatMap.keySet()) {
                LotteryStatistic ls = lotteryStatMap.get(boSo);
                ls.setTinhQuay(boSoTrungThuong.getBoSoTrungThuongPK().getMaTinh());

                Date ngayQuay = boSoTrungThuong.getBoSoTrungThuongPK().getNgayQuay();
                if (cacBoSo.contains(boSo)) {
                    ls.getWinDates().add(ngayQuay);
                }
            }
        }
        list = new ArrayList<LotteryStatistic>();
        for (String boSo : lotteryStatMap.keySet()) {
            list.add(lotteryStatMap.get(boSo));
        }
    }

    public Map<String, String> getProvinceMap() {
        return provinceMap;
    }

    public void setProvinceMap(Map<String, String> provinceMap) {
        this.provinceMap = provinceMap;
    }

    public String getSelectedProvince() {
        return selectedProvince;
    }

    public void setSelectedProvince(String selectedProvince) {
        this.selectedProvince = selectedProvince;
    }

    public List<LotteryStatistic> getList() {
        return list;
    }

    public void setList(List<LotteryStatistic> list) {
        this.list = list;
    }
}
